/**
 * Created by wzq
 */

'use strict';
var dao = require('./mysqlDao/checkout.js');

var service = {};
module.exports = service;


service.add = function*(json) {
  var retObj = yield dao.save(json);
  return retObj;
};

service.find = function*(json) {
  var where = {};
  if (typeof json.vid !== 'undefined') where.vid = json.vid;
  if (typeof json.begin !== 'undefined') where.begin = {gte: new Date(json.begin).getTime() / 1000};
  if (typeof json.end !== 'undefined') where.end = {lte: new Date(json.end).getTime() / 1000};
  if (typeof json.status !== 'undefined') {
    if (json.status === 'checked')where.status = 2;
    else if (json.status === 'unchecked') where.$or = [{status: 1}, {status: 0}];
  }

  var retObj = yield dao.query(where);
  return retObj;
};


service.detail = function*(json) {
  var retObj = yield dao.detail(json);
  return retObj;
};

/**
 * 标记成已结算
 * status =2 已结算 =0未结算, =1正在结算
 * @param json {id:xx,status:xx}
 */
service.setStatus = function*(json) {
  console.log('setStatus', json);
  var id = json.id;
  var status = json.status;
  var st = 0;
  if (status === 'unchecked') st = 0;
  else if (status === 'checking') st = 1;
  else if (status === 'checked') st = 2;

  var where = {status: {ne: st}};
  where.id = (Array.isArray(id)) ? {$in: id} : id;
  console.log(where);
  var update = {status: st};
  var retObj = yield dao.update(where, update);
  return retObj;
};

service.checkOneMonth = function*(json) {
  var year = json.year;
  var month = json.month;
  var retObj = yield dao.checkOneMonth(year, month);
  return retObj;
};